export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome() {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要喝一杯卡布奇诺', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

export function millsToTime(mills) {
  if (!mills) {
    return ''
  }
  const s = mills / 1000
  if (s < 60) {
    return s.toFixed(0) + ' 秒'
  }
  const m = s / 60
  if (m < 60) {
    return m.toFixed(0) + ' 分钟'
  }
  const h = m / 60
  if (h < 24) {
    return h.toFixed(0) + ' 小时'
  }
  const d = h / 24
  if (d < 30) {
    return d.toFixed(0) + ' 天'
  }
  const month = d / 30
  if (month < 12) {
    return month.toFixed(0) + ' 个月'
  }
  const year = month / 12
  return year.toFixed(0) + ' 年'
}

export function isPhone(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  var regu = '^1[0-9]{10}$'//手机号码验证regEx:第一位数字必须是1，11位数字
  var re = new RegExp(regu)
  if (re.test(value)) {
    callback()
  } else {
    callback('请输入正确的手机号')
  }
}

export function phoneRules(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  var regu = /^((0\d{2,3}(-?)\d{7,8})|(1[3456789]\d{9}))$/ //电话校验，包含固话
  var re = new RegExp(regu)
  if (re.test(value)) {
    callback()
  } else {
    callback('请输入正确的电话')
  }
}
export function isIdNumber(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  //正则表达式
  var regu = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/
  //身份证校验
  var re = new RegExp(regu)
  if (re.test(value)) {
    callback()
  } else {
    callback('请正确输入身份证！')
  }
}

/**
 * ip地址校验
 */
export function isIp(rule, value, callback) {
  console.log(rule)
  if (value == '' || value == null) {
    callback()
    return
  }
  //正则表达式
  var regu = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  //身份证校验
  var re = new RegExp(regu)
  if (re.test(value)) {
    callback()
  } else {
    callback('请输入正确ip地址！')
  }
}

/**
 * 长度校验
 */
export function checkValueLength(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  var len = value.length
  var maxLength = rule.maxLength
  for (var i = 0; i < value.length; i++) {
    //如果是中文长度+1
    if ((value.charCodeAt(i) & 0xff00) != 0) {
      len += 1
    }
  }
  if (len > maxLength) {
    callback('输入超出长度限制')
  } else {
    callback()
  }
}
export function isJSON(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  if (typeof value == 'string') {
    try {
      var obj = JSON.parse(value)
      if (typeof (obj) === 'object' && obj) {
        callback()
        return
      } else {
        callback('请输入json格式字符串')
        return
      }
    } catch (e) {
      callback('请输入json格式字符串')
      return
    }
  } else {
    callback('请输入json格式字符串')
  }
}

export function isJSONArray(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  if (!value.startsWith('[') || !value.endsWith(']')) {
    callback('请输入jsonArray格式字符串')
    return
  }
  if (typeof value == 'string') {
    try {
      var obj = JSON.parse(value)
      if (typeof (obj) === 'object' && obj) {
        callback()
        return
      } else {
        callback('请输入json格式字符串')
        return
      }
    } catch (e) {
      callback('请输入json格式字符串')
      return
    }
  } else {
    callback('请输入json格式字符串')
  }
}

export function formatter(number) {
  const numbers = number
    .toString()
    .split('')
    .reverse()
  const segs = []
  while (numbers.length) segs.push(numbers.splice(0, 3).join(''))
  return segs
    .join(',')
    .split('')
    .reverse()
    .join('')
}


export function urlAndIpCheck(rule, value, callback) {
  if (value == '' || value == null) {
    callback()
    return
  }
  var ip = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
  var reIp = new RegExp(ip)
  var url = '^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+'
  var reUrl = new RegExp(url)
  if (reIp.test(value) || reUrl.test(value)) {
    callback()
  } else {
    callback('请输入正确地址！')
  }
}

